﻿@page "/admin/apiscopes"

@attribute [Authorize(Roles = "Admin")]

@inject IHttpService _httpService
@inject IPopupService PopupService
@inject INavigationService _navigationService

@using CnGalWebSite.IdentityServer.Models.ViewModels.Roles

<DataTableCard TModel="ApiScopeOverviewModel" Headers="_headers" ApiUrl="@(_baseUrl+"list")" Title="@($"{_name}列表")" OnClickAdd="AddItem" @ref="dataTableCard">
    <ItemColContent>
        @if (context.Header.Value == "actions")
        {
            <MIcon Color="success"
                   Class="mr-2"
                   OnClick="()=>EditItem(context.Item)">mdi-pencil</MIcon>
        }
        else if (context.Header.Value == "required")
        {
            <CnGalWebSite.IdentityServer.Admin.Shared.Components.ApiScopes.RequiredChip Required="@context.Item.Required" />
        }
        else if (context.Header.Value == "enable")
        {
            <CnGalWebSite.IdentityServer.Admin.Shared.Components.Clients.EnableChip Enabled="@context.Item.Enabled" />
        }
        else
        {
            @context.Value
        }
    </ItemColContent>
</DataTableCard>

<MDialog @bind-Value="_editDialog"
         MaxWidth="1200">
    <CnGalWebSite.IdentityServer.Admin.Shared.Components.ApiScopes.EditCard Id="_editedItemId" OnEdited="OnEdited" @ref="editCard" />
</MDialog>

@code {
    private bool _editDialog;
    private int _editedItemId;
    private bool _detailDialog;

    private string _baseUrl = "api/apiscopes/";
    private string _name = "ApiScope";

    CnGalWebSite.IdentityServer.Admin.Shared.Components.ApiScopes.EditCard editCard;

    private List<DataTableHeader<ApiScopeOverviewModel>> _headers = new List<DataTableHeader<ApiScopeOverviewModel>>
{
          new ()
          {
            Text= "Id",
            Align= DataTableHeaderAlign.Start,
            Value= nameof(ApiScopeOverviewModel.Id)
          },
          new (){ Text= "名称", Value= nameof(ApiScopeOverviewModel.Name)},
          new (){ Text= "显示名称", Value= nameof(ApiScopeOverviewModel.DisplayName)},
          new (){ Text= "简介", Value= nameof(ApiScopeOverviewModel.Description)},
          new (){ Text= "必选", Value= "required"},
          new (){ Text= "启用", Value= "enable" },
          new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    DataTableCard<ApiScopeOverviewModel> dataTableCard;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    protected override async Task OnInitializedAsync()
    {

    }

    public async void EditItem(ApiScopeOverviewModel item)
    {
        _editedItemId = item.Id;
        _editDialog = true;
        StateHasChanged();
        if (editCard != null)
        {
            await editCard.Refresh();
        }
    }

    public void OnEdited()
    {
        _editDialog = false;
        StateHasChanged();
        if (dataTableCard != null)
        {
            dataTableCard.GetDataFromApi();
        }
    }

    public async Task AddItem()
    {
        _editedItemId = 0;
        _editDialog = true;
        StateHasChanged();
        if (editCard != null)
        {
            await editCard.Refresh();
        }
    }

}
